package com.zwan.oam_rtc.service;

import com.alibaba.fastjson.JSON;
import com.zwan.oam_rtc.bean.OamSettings;
import com.zwan.oam_rtc.bean.entity.CreateRoomData;
import com.zwan.oam_rtc.bean.entity.InviteSipData;
import com.zwan.oam_rtc.bean.req.*;
import com.zwan.oam_rtc.constants.Const;
import com.zwan.oam_rtc.imps.JavaImp;
import com.zwan.oam_rtc.imps.UserStatusHandler;
import com.zwan.oam_rtc.utils.process.ProcUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Slf4j
@Service
public class RtcmsService {

    @Autowired
    OamSettings oamSettings;

    private UserStatusHandler getJimpHandler(){
        JavaImp jimp = JavaImp.getInstance();
        return (UserStatusHandler)jimp.getHandlerInstance(UserStatusHandler.class);
    }

    public String getConfListData(){
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_GET_CONF_LIST, "{}", true);
    }

    public String getConfBriefListData(){
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_GET_CONF_LIST_BRIEF, "{}", true);
    }

    public String getConfData(String room) {
        String req = String.format("{\"room\":\"%s\"}", room);
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_GET_CONF, req, true);
    }

    public String getUserData(QueryConfMemberInfoReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_GET_USER_LIST, JSON.toJSONString(req), true);
    }

    public String confSMS(ConfSMSReq req) {
        UserStatusHandler handler = getJimpHandler();
        log.info("will send:"+JSON.toJSONString(req));
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_CONF_SMS, JSON.toJSONString(req), true);
    }

    public String zwmpList() {
        UserStatusHandler handler = getJimpHandler();
        String rep = handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_MRFP_LIST, JSON.toJSONString(null), true);
        return rep;
    }

    public String zwmpDetail(zwmpDetailReq req) {
        UserStatusHandler handler = getJimpHandler();
        String rep =  handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_MRFP_DETAIL, JSON.toJSONString(req), true);
        return rep;
    }

    public String queryDevice() {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_QUERY_DEVICE, JSON.toJSONString(null), true);
    }

    public String inviteSip(InviteSipData req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_INVITE_SIP, JSON.toJSONString(req), true);
    }

    public String startRecord(startRecordReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_RECORD_START, JSON.toJSONString(req), true);
    }

    public String stopRecord(stopRecordReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_RECORD_STOP, JSON.toJSONString(req), true);
    }

    public String switchRecord(switchRecordReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_RECORD_SWITCH, JSON.toJSONString(req), true);
    }

    public String snapRecord(snapRecordReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_RECORD_SNAP, JSON.toJSONString(req), true);
    }

    public String registerRoomObserver(roomObservorReq req) {
        UserStatusHandler handler = getJimpHandler();
        UserStatusHandler.setRoomObervers(req);
        UserStatusHandler.printRoomOberver();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_REGISTER_ROOM_OBSERVER, JSON.toJSONString(null), true);
    }

    public String registerObserver() {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_REGISTER_OBSERVER, JSON.toJSONString(null), true);
    }

    public String sendSIPMessage(sendSipReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_SIP_MESSAGE, JSON.toJSONString(req), true);
    }

    public String sendSIPSubscribe(sendSipReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_SIP_SUBSCRIBE, JSON.toJSONString(req), true);
    }

    public String securityURL(securityUrlReq req) {
        String time = String.valueOf(System.currentTimeMillis() / 1000 + Long.valueOf(600));// +600代表600秒后地址失效
        String file;
        String secdir;
        if(req.getType().equals("hls")){
            secdir = oamSettings.getHlsSecurityURI();     //nginx上配置的加密链接location
            if(req.getUrl().startsWith("/")){
                file = req.getUrl().substring(1,req.getUrl().length()-1);
            }else{
                file = req.getUrl();
            }

            String url = file+oamSettings.getHlsKey();
            log.info(url);
            String md5 = DigestUtils.md5DigestAsHex(url.getBytes());
            return "https://meeting.zwan.com.cn:2237"+oamSettings.getHlsSecurityURI()+md5+"/"+file;
        } else if(req.getType().equals("mp4")){
            secdir = oamSettings.getRecordSecurityURI();
            if(!req.getUrl().startsWith("/")){
                file = secdir+req.getUrl();
            } else{
                file = req.getUrl().substring(1,req.getUrl().length()-1);
                file = secdir+file;
            }
            String url = "customSecretKey" + file + time;
            log.info(url);
            String md5 = Base64.encodeBase64URLSafeString(DigestUtils.md5Digest(url.getBytes()));
            return "https://meeting.zwan.com.cn:2237"+file+"?st=" + md5 + "&e=" + time;
        } else{
            return null;
        }

    }

    public String queryQOS(queryQosReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_QUERY_QOS, JSON.toJSONString(req), true);
    }

    public String zwmpUsage(zwmpUsageReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_QUERY_ZWMP_USAGE, JSON.toJSONString(req), true);
    }

    public String zwmpPort(zwmpUsageReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_QUERY_ZWMP_PORT, JSON.toJSONString(req), true);
    }

    public String zwmpChans(zwmpUsageReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_QUERY_ZWMP_CHANS, JSON.toJSONString(req), true);
    }

    public String zwmpChanDetail(zwmpChanDetailReq req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_QUERY_ZWMP_CHAN_DETAIL, JSON.toJSONString(req), true);
    }

    public String createRoom(CreateRoomData req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_CREATE_ROOM, JSON.toJSONString(req), true);
    }

    public String deleteRoom(CreateRoomData req) {
        UserStatusHandler handler = getJimpHandler();
        return handler.sendMessage2Rtcms((short)Const.OAM_COMMAND_DELETE_ROOM, JSON.toJSONString(req), true);
    }

    public String getLicense(String cpuid) {
        return ProcUtil.getLicense(cpuid);
    }
}
